package generator.statistics;

import generator.driver.GeneratorStatistics;
import generator.structure.Graph;
import generator.structure.Pair;
import generator.structure.Vertex;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

public class GetClasses {

	ArrayList<ArrayList<Integer>> circle = new ArrayList<ArrayList<Integer>>();
	ArrayList<ArrayList<Integer>> overlap = new ArrayList<ArrayList<Integer>>();
	ArrayList<ArrayList<Integer>> MUTUAL = new ArrayList<ArrayList<Integer>>();

	private HashMap<Integer,Integer> Exist = new HashMap<Integer,Integer>();
	Vertex vertex;
	
	public void classify(Set<Pair<Integer,Integer>> setInput){
		
		int nVerts = setInput.size() * 2;
		Graph myGraph = new Graph(nVerts,false);
		int done = 0;//How many node have finished their procedure.
		int i = 1;
		
		Iterator<Pair<Integer,Integer>> iter = setInput.iterator();
		Pair<Integer,Integer> tempPair1 = iter.next();
		vertex = new Vertex(tempPair1.first_);
		myGraph.addVertex(vertex);	
		Exist.put(tempPair1.first_, 0);
		
		vertex = new Vertex(tempPair1.second_);
		myGraph.addVertex(vertex);	
		Exist.put(tempPair1.second_, 1);
		
		myGraph.addEdge(Exist.get(tempPair1.first_), Exist.get(tempPair1.second_));
//		System.out.println("tempPair1.first_:" + tempPair1.first_ + " tempPair1.second_:" +tempPair1.second_);
		for(; iter.hasNext();){
			
			Pair<Integer,Integer> tempPair = iter.next();
			
			if(!Exist.containsKey(tempPair.first_)){
				
				i++;
				vertex = new Vertex(tempPair.first_);
				myGraph.addVertex(vertex);	
				Exist.put(tempPair.first_, i);
				
			}

			if(!Exist.containsKey(tempPair.second_)){
				
				i++;
				vertex = new Vertex(tempPair.second_);
				myGraph.addVertex(vertex);	
				Exist.put(tempPair.second_, i);
				
			}
			
	//		System.out.println("tempPair.first_:" + tempPair.first_ + " tempPair.second_:" +tempPair.second_);
			myGraph.addEdge(Exist.get(tempPair.first_), Exist.get(tempPair.second_));
			
		}
		
		for (Iterator<?> iterator = myGraph.dfsIterator(done,0); iterator.hasNext();) {			
			vertex = (Vertex) iterator.next();
		
			if(vertex != null){
				done++;
			//	System.out.println("done: " + done);
			} else {
			//	System.out.println("again");
				for(i = 0;i < nVerts;i++){
					if(!myGraph.vertexList[i].isVisted) break;
				}
				myGraph.count--;
				iterator = myGraph.dfsIterator(done, i);
			}		
		}
		
	//	myGraph.displayGraph();
		
		circle = myGraph.getCircle();
//		circle = new ArrayList<ArrayList<Integer>>();
		
//		ArrayList a = new ArrayList();
//		a.add(10);
//		ArrayList<ArrayList<Integer>> b = new ArrayList<ArrayList<Integer>>();
//		b.add(a);
//		circle = b;
		
		overlap = myGraph.getOverlap();
		MUTUAL = myGraph.getMutual();
		
		System.out.println("circle-------------------------");
		for(int j = 0; j < circle.size();j++){
			ArrayList<Integer> tempArray = circle.get(j);
			for(i = 0; i < tempArray.size(); i++){
				System.out.println(j + ": " + tempArray.get(i));
			}
		}
		
		System.out.println("overlap-------------------------");
		for(int j = 0; j < overlap.size();j++){
			ArrayList<Integer> tempArray = overlap.get(j);
			for(i = 0; i < tempArray.size(); i++){
				System.out.println(j + ": " + tempArray.get(i));
			}
		}
		
		System.out.println("MUTUAL-------------------------");
		for(int j = 0; j < MUTUAL.size();j++){
			ArrayList<Integer> tempArray = MUTUAL.get(j);
			for(i = 0; i < tempArray.size(); i++){
				System.out.println(j + ": " + tempArray.get(i));
			}
		}
		
		//add to these classes
		GeneratorStatistics.mutual.setGroup(MUTUAL);
		GeneratorStatistics.circle.setGroup(circle);
		GeneratorStatistics.overlap.setGroup(overlap);
	}
}
